package com.ruoyi.system.service.impl;

import java.math.RoundingMode;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.AdminPumpRecord;
import com.ruoyi.system.domain.AdminUserFlowing;
import com.ruoyi.system.domain.AdminWeixinUser;
import com.ruoyi.system.mapper.AdminUserFlowingMapper;
import com.ruoyi.system.mapper.AdminWeixinUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AdminUpdownBranchMapper;
import com.ruoyi.system.domain.AdminUpdownBranch;
import com.ruoyi.system.service.IAdminUpdownBranchService;

/**
 * 上下分管理Service业务层处理
 *
 * @author ruoyi
 * @date 2020-11-10
 */
@Service
public class AdminUpdownBranchServiceImpl implements IAdminUpdownBranchService
{
    @Autowired
    private AdminUpdownBranchMapper adminUpdownBranchMapper;

    @Autowired
    private AdminWeixinUserMapper adminWeixinUserMapper;

    @Autowired
    private AdminUserFlowingMapper adminUserFlowingMapper;

    @Value("${send.url}")
    private String sendUrl;


    /**
     * 查询上下分管理
     *
     * @param id 上下分管理ID
     * @return 上下分管理
     */
    @Override
    public AdminUpdownBranch selectAdminUpdownBranchById(Long id)
    {
        return adminUpdownBranchMapper.selectAdminUpdownBranchById(id);
    }

    @Override
    public AdminUpdownBranch selectAdminUpdownBranchByWeixinIdAndRoom(AdminUpdownBranch adminUpdownBranch) {
        return adminUpdownBranchMapper.selectAdminUpdownBranchByWeixinIdAndRoom(adminUpdownBranch);
    }

    /**
     * 查询上下分管理列表
     *
     * @param adminUpdownBranch 上下分管理
     * @return 上下分管理
     */
    @Override
    public List<AdminUpdownBranch> selectAdminUpdownBranchList(AdminUpdownBranch adminUpdownBranch)
    {
        return adminUpdownBranchMapper.selectAdminUpdownBranchList(adminUpdownBranch);
    }

    @Override
    public List<AdminUpdownBranch> selectAdminUpdownBranchListByAdmin(AdminUpdownBranch adminUpdownBranch) {
        return adminUpdownBranchMapper.selectAdminUpdownBranchListByAdmin(adminUpdownBranch);
    }

    /**
     * 新增上下分管理
     *
     * @param adminUpdownBranch 上下分管理
     * @return 结果
     */
    @Override
    public int insertAdminUpdownBranch(AdminUpdownBranch adminUpdownBranch)
    {
        adminUpdownBranch.setCreateTime(DateUtils.getNowDate());
        return adminUpdownBranchMapper.insertAdminUpdownBranch(adminUpdownBranch);
    }

    /**
     * 修改上下分管理
     *
     * @param adminUpdownBranch 上下分管理
     * @return 结果
     */
    @Override
    public int updateAdminUpdownBranch(AdminUpdownBranch adminUpdownBranch)
    {
        adminUpdownBranch.setUpdateTime(DateUtils.getNowDate());
        return adminUpdownBranchMapper.updateAdminUpdownBranch(adminUpdownBranch);
    }

    /**
     * 批量删除上下分管理
     *
     * @param ids 需要删除的上下分管理ID
     * @return 结果
     */
    @Override
    public int deleteAdminUpdownBranchByIds(Long[] ids)
    {
        return adminUpdownBranchMapper.deleteAdminUpdownBranchByIds(ids);
    }

    /**
     * 删除上下分管理信息
     *
     * @param id 上下分管理ID
     * @return 结果
     */
    @Override
    public int deleteAdminUpdownBranchById(Long id)
    {
        return adminUpdownBranchMapper.deleteAdminUpdownBranchById(id);
    }

    @Override
    public int audit(AdminUpdownBranch adminUpdownBranch) {
        AdminUpdownBranch adminUpdownBranch1 = adminUpdownBranchMapper.selectAdminUpdownBranchById(adminUpdownBranch.getId());
        AdminWeixinUser adminWeixinUser2 = new AdminWeixinUser();
        adminWeixinUser2.setWexinId(adminUpdownBranch1.getWexinId());
        adminWeixinUser2.setRoom(adminUpdownBranch1.getRoom());
        AdminWeixinUser adminWeixinUser = adminWeixinUserMapper.selectAdminWeixinUserByWeChatIdAndRoom(adminWeixinUser2);
        AdminUserFlowing adminUserFlowing = new AdminUserFlowing();
        adminUserFlowing.setRoom(adminUpdownBranch1.getRoom());
        adminUserFlowing.setUserId(adminWeixinUser.getId());
        adminUserFlowing.setWexinId(adminWeixinUser.getWexinId());
        adminUserFlowing.setWexinName(adminWeixinUser.getWexinName());
        adminUserFlowing.setCreateTime(DateUtils.getNowDate());
        adminUserFlowing.setPayPrice(adminUpdownBranch1.getPrice());
        if (adminUpdownBranch.getAuditStatus()==1) {
            //审核成功
            if (adminUpdownBranch1.getBranchType()==1) {
                //上分审核成功
                adminWeixinUser.setPrice(adminWeixinUser.getPrice().add(adminUpdownBranch1.getPrice()));
                adminUserFlowing.setStartPrice(adminWeixinUser.getPrice());
                adminUserFlowing.setEndPrice(adminWeixinUser.getPrice().add(adminUpdownBranch1.getPrice()));
                adminUserFlowing.setPayType(2);
                adminUserFlowing.setRemark("用户上分");

            }else if(adminUpdownBranch1.getBranchType()==2){
//                if (new Double(adminWeixinUser.getPrice().subtract(adminUpdownBranch1.getPrice()).toString())<0) {
                if(adminWeixinUser.getPrice().setScale(2, RoundingMode.HALF_UP).compareTo(adminUpdownBranch1.getPrice().setScale(2, RoundingMode.HALF_UP))<0){
                    return 0;
                }
                adminWeixinUser.setPrice(adminWeixinUser.getPrice().subtract(adminUpdownBranch1.getPrice()));
                adminUserFlowing.setStartPrice(adminWeixinUser.getPrice());
                adminUserFlowing.setEndPrice(adminWeixinUser.getPrice().subtract(adminUpdownBranch1.getPrice()));
                adminUserFlowing.setPayType(1);
                adminUserFlowing.setRemark("用户下分提现");
                //下分审核成功
            }
            adminWeixinUserMapper.updateAdminWeixinUser(adminWeixinUser);
            adminUserFlowingMapper.insertAdminUserFlowing(adminUserFlowing);
        }
        adminUpdownBranchMapper.updateAdminUpdownBranch(adminUpdownBranch);
        AdminWeixinUser adminWeixinUser1 = adminWeixinUserMapper.selectAdminWeixinUserByWeChatIdAndRoom(adminWeixinUser2);
        String fen = adminUpdownBranch1.getBranchType() == 1 ? "上分" : "下分";
        String state = adminUpdownBranch.getAuditStatus() == 1 ? "成功" : "失败";
        String finalSendMsg="@"+adminWeixinUser.getWexinName()+" "+fen+adminUpdownBranch1.getPrice()+state+",剩:"+adminWeixinUser1.getPrice();
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpUtils.sendPost(sendUrl,"sendMsg="+ finalSendMsg+"&room="+adminWeixinUser1.getRoom());
            }
        }).start();
        return 1;
    }
}
